package com.zhz.employee.biz;

import com.zhz.common.enums.BizResponseEnum;
import com.zhz.employee.contract.request.EmployeeSaveJobsInfoRequest;
import com.zhz.employee.repository.service.UserCompanyJobsService;
import com.zhz.model.employee.contract.dto.UserCompanyJobsDTO;
import com.zhz.model.employee.contract.param.UserCompanyJobsQueryParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 岗位信息
 *
 * @author zhouhengzhe
 * @date 2023/5/30
 */
@Component
public class UserCompanyJobsBizService {

    private final UserCompanyJobsService userCompanyJobsService;

    @Autowired
    public UserCompanyJobsBizService(UserCompanyJobsService userCompanyJobsService) {
        this.userCompanyJobsService = userCompanyJobsService;
    }


    /**
     * 根据ID获取岗位信息
     *
     * @param id 主键ID
     * @return 岗位信息
     */
    public UserCompanyJobsDTO findById(String id) {
        UserCompanyJobsDTO userCompanyJobsDTO = userCompanyJobsService.get(id);
        BizResponseEnum.USER_COMPANY_JOBS_NOT_EXIST.assertNotNull(userCompanyJobsDTO);
        return userCompanyJobsDTO;
    }

    /**
     * 员工岗位信息保存
     *
     * @param employeeSaveJobsInfoRequest 员工岗位信息保存请求
     * @param companyId                   公司ID
     */
    public void save(EmployeeSaveJobsInfoRequest employeeSaveJobsInfoRequest, String companyId) {
        UserCompanyJobsDTO userCompanyJobsDTO = new UserCompanyJobsDTO();
        //更新员工岗位信息
        userCompanyJobsDTO.setUserId(employeeSaveJobsInfoRequest.getUserId());
        userCompanyJobsDTO.setCompanyId(companyId);
        userCompanyJobsService.save(userCompanyJobsDTO);
    }

    /**
     * 员工岗位信息读取
     *
     * @param id 员工岗位信息读取请求
     * @param companyId                   公司ID
     * @return
     */
    public UserCompanyJobsDTO findInfoById(String id, String companyId) {
        UserCompanyJobsDTO userCompanyJobsDTO = userCompanyJobsService.getOne(
                UserCompanyJobsQueryParam
                        .builder()
                        .id(id)
                        .build()
        );
        if (Objects.nonNull(userCompanyJobsDTO)) {
            return userCompanyJobsDTO;
        }
        userCompanyJobsDTO = UserCompanyJobsDTO
                .builder()
                .userId(id)
                .build();
        if (StringUtils.isNotBlank(companyId)) {
            userCompanyJobsDTO.setCompanyId(companyId);
        }
        return userCompanyJobsDTO;
    }
}
